package cn.o.app.io;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Parcelable;

public class OShare {

	public static final String FILTER_SINA_WEIBO = "com.sina.weibo";

	public static final String FILTER_WEIXIN = "com.tencent.mm";

	public static final String FILTER_QQ = "com.tencent.mobileqq";

	public static final String FILTER_TWITTER = "com.twitter.android";

	public static final String FILTER_FACEBOOK = "com.facebook.katana";

	public static final String FILTER_KAIXIN = "com.kaixin";

	public static final String FILTER_GOOGLE_PLUS = "com.google.android.apps.plus";

	public static final String FILTER_RENREN = "com.renren.mobile.android";

	public static final String FILTER_QZONE = "com.qzone";

	public static final String FILTER_SOHU_WEIBO = "com.cola.twisohu";

	public static final String FILTER_TENCENT_WEIBO = "com.tencent.WBlog";

	public static final String FILTER_YIXIN = "im.yixin";

	protected String mTitle;

	protected String mText;

	protected String mImagePath;

	protected Context mContext;

	protected String mChooserTitle;

	protected List<String> mChooserFilters;

	public OShare(Context context) {
		mContext = context;
	}

	public String getTitle() {
		return mTitle;
	}

	public void setTitle(String title) {
		mTitle = title;
	}

	public String getText() {
		return mText;
	}

	public void setText(String text) {
		this.mText = text;
	}

	public String getImagePath() {
		return mImagePath;
	}

	public void setImagePath(String imagePath) {
		mImagePath = imagePath;
	}

	public String getChooserTitle() {
		return mChooserTitle;
	}

	public void setChooserTitle(String chooserTitle) {
		mChooserTitle = chooserTitle;
	}

	public void addChooserFilter(String filter) {
		if (mChooserFilters == null) {
			mChooserFilters = new ArrayList<String>();
		}
		mChooserFilters.add(filter);
	}

	public void removeChooserFilter(String filter) {
		if (mChooserFilters == null) {
			return;
		}
		if (mChooserFilters.contains(filter)) {
			return;
		}
		mChooserFilters.remove(filter);
	}

	public void addDefaultFilters() {
		mChooserFilters = new ArrayList<String>();
		mChooserFilters.add(FILTER_SINA_WEIBO);
		mChooserFilters.add(FILTER_WEIXIN);
		mChooserFilters.add(FILTER_QQ);
		mChooserFilters.add(FILTER_TWITTER);
		mChooserFilters.add(FILTER_FACEBOOK);
		mChooserFilters.add(FILTER_KAIXIN);
		mChooserFilters.add(FILTER_GOOGLE_PLUS);
		mChooserFilters.add(FILTER_RENREN);
		mChooserFilters.add(FILTER_QZONE);
		mChooserFilters.add(FILTER_SOHU_WEIBO);
		mChooserFilters.add(FILTER_TENCENT_WEIBO);
		mChooserFilters.add(FILTER_YIXIN);
	}

	public boolean isPackageToBeShare(ActivityInfo activityInfo) {
		if (mChooserFilters == null) {
			return true;
		}
		if (mChooserFilters.size() == 0) {
			return true;
		}
		for (String filter : mChooserFilters) {
			if (activityInfo.packageName.contains(filter)) {
				return true;
			}
		}
		return false;
	}

	public boolean shareText() {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		List<ResolveInfo> resolveInfos = mContext.getPackageManager()
				.queryIntentActivities(intent,
						PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
		if (resolveInfos.isEmpty()) {
			return false;
		}
		List<Intent> targetShareIntents = new ArrayList<Intent>();
		List<String> targetSharePackages = new ArrayList<String>();
		for (ResolveInfo resolveInfo : resolveInfos) {
			ActivityInfo activityInfo = resolveInfo.activityInfo;
			if (!isPackageToBeShare(activityInfo)) {
				continue;
			}
			if (targetSharePackages.contains(activityInfo.packageName)) {
				continue;
			}
			Intent targeted = new Intent(Intent.ACTION_SEND);
			targeted.setType("text/plain");
			targeted.setPackage(activityInfo.packageName);
			targeted.setClassName(activityInfo.packageName, activityInfo.name);
			if (mTitle != null) {
				targeted.putExtra(Intent.EXTRA_SUBJECT, mTitle);
				targeted.putExtra(Intent.EXTRA_TITLE, mTitle);
			}
			if (mText != null) {
				targeted.putExtra(Intent.EXTRA_TEXT, mText);
			}
			targetShareIntents.add(targeted);
			targetSharePackages.add(activityInfo.packageName);
		}
		if (targetShareIntents.size() == 0) {
			return false;
		}
		Intent chooserIntent = Intent.createChooser(targetShareIntents.get(0),
				mChooserTitle);
		if (chooserIntent == null) {
			return false;
		}
		chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
				targetShareIntents.toArray(new Parcelable[] {}));
		try {
			mContext.startActivity(chooserIntent);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean shareImage() {
		Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
		intent.setType("image/*");
		List<ResolveInfo> resolveInfos = mContext.getPackageManager()
				.queryIntentActivities(intent,
						PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
		if (resolveInfos.isEmpty()) {
			return false;
		}
		List<Intent> targetShareIntents = new ArrayList<Intent>();
		for (ResolveInfo resolveInfo : resolveInfos) {
			ActivityInfo activityInfo = resolveInfo.activityInfo;
			if (!isPackageToBeShare(activityInfo)) {
				continue;
			}
			Intent targeted = new Intent(Intent.ACTION_SEND_MULTIPLE);
			targeted.setType("image/*");
			targeted.setPackage(activityInfo.packageName);
			targeted.setClassName(activityInfo.packageName, activityInfo.name);
			if (mTitle != null) {
				targeted.putExtra(Intent.EXTRA_SUBJECT, mTitle);
				targeted.putExtra(Intent.EXTRA_TITLE, mTitle);
			}
			if (mText != null) {
				targeted.putExtra(Intent.EXTRA_TEXT, mText);
				intent.putExtra("Kdescription", mText);// 微信朋友圈
			}
			if (mImagePath != null) {
				targeted.putExtra(Intent.EXTRA_STREAM,
						Uri.fromFile(new File(mImagePath)));
			}
			targetShareIntents.add(targeted);
		}
		if (targetShareIntents.size() == 0) {
			return false;
		}
		Intent chooserIntent = Intent.createChooser(targetShareIntents.get(0),
				mChooserTitle);
		if (chooserIntent == null) {
			return false;
		}
		chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
				targetShareIntents.toArray(new Parcelable[] {}));
		try {
			mContext.startActivity(chooserIntent);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
